<?php
namespace app\common\model\mysql;

use think\Model;
use think\model\concern\SoftDelete;

class CarUserSalary extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';


    public function carUser(){
        return $this->belongsTo('app\common\model\mysql\CarUser','user_id','id');
    }

    public function getCarUserSalaryById($id){
        if(empty($id)){
            return false;
        }
        $where = [
            'id' => trim($id),
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getCarUserList(){
        $result = $this->where(['delete_time' => null])->select();
        return $result;
    }
    //获取列表
    public function getCarUserDataByPage($where = []){
        $whereData = ['delete_time' => null];
        if(!empty($where)){
            $whereData = array_merge($whereData, $where);
        }
        $whereBetween = $whereData['enter_date'];
        unset($whereData['enter_date']);
        $order = [
            'id' => 'desc',
        ];
        $result = $this->where($whereData)->whereBetween('enter_date',$whereBetween)->order($order)->select();
        return $result;
    }

    //修改用户信息
    /**
        * 根据id获取用户信息
     */
    public function updateById($id,$data = []){
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->save($data);
    }
    //创建用户信息
    public function insertCarUserData($data = []){
        if(empty($data) || !is_array($data)){
            return false;
        }
        return $this->insertGetId($data);
    }
    //软删除用户
    public function deleteById($id){
        if(empty($id)){
            return false;
        }
        $where = [
            'id' => $id,
        ];
        return $this->where($where)->update(['delete_time' => time()]);
    }

    public function deleteByUserId($id){
        if(empty($id)){
            return false;
        }
        $where = [
            'user_id' => $id,
        ];
        return $this->where($where)->update(['delete_time' => time()]);
    }

}